import { useModule } from "@/func/createMod";
/*
 * @Author: Black / 李来然
 * @Date: 2021-12-20 15:36:58
 * @LastEditors: Black / 李来然
 * @LastEditTime: 2021-12-20 15:38:03
 * @Description: 工具类
 */

import { StatusCode, statusCodeDescription } from "@/code";
import { BaseCallBackParams, BaseOpt, EvtEnmu } from "@/types";
import { exec } from "child_process";

/**
 * 获取元素的纵坐标（相对于窗口）
 * @param {object} e event
 * @return	top值
 */
function getTop(e: any) {
  let offset = e.offsetTop;

  if (e.offsetParent != null && e.offsetParent)
    offset += getTop(e.offsetParent);

  return offset;
}

/**
 * 获取元素的横坐标（相对于窗口）
 * @param {object} e event
 * @return	left值
 */
function getLeft(e: any) {
  let offset = e.offsetLeft;

  if (e.offsetParent != null && e.offsetParent)
    offset += getLeft(e.offsetParent);

  return offset;
}
// addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
function getCallBackParam<T = null>(
  statusCode: StatusCode,
  data: T = null as T
): BaseCallBackParams<T> {
  let result = {
    code: statusCode,
    message: statusCodeDescription[statusCode],
    data: data,
    ok: true,
  };
  return result;
}

/**
 *Base64字符串转二进制
 */
function dataURLtoBlob(dataurl: any) {
  var arr = dataurl.split(","),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {
    type: mime,
  });
}
// base64转hex
function base64ToHex(base64: string) {
  let str = window.atob(base64);
  let hex = "";
  for (let i = 0; i < str.length; i++) {
    let tmp = str.charCodeAt(i).toString(16);
    if (tmp.length == 1) {
      tmp = "0" + tmp;
    }
    hex += tmp;
  }
  return hex;
}

export const DirStruct = async (strPath: string): Promise<string[]> => {
  const { common } = useModule();
  if (!common) {
    throw new Error(`没有${common}模块`);
  }
  let ret = await common.TraversalDir(strPath);
  if(ret) {
    let retNum = JSON.parse(ret);
    return retNum.file;
  }
  return [];
};

export { getTop, getLeft, getCallBackParam, dataURLtoBlob, base64ToHex };
